home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / dist / remote-multi.shar / remote_utils.c < prev   
Encoding:
C/C++ Source or Header  |  1989-09-20  |  6.6 KB  |  339 lines

  1. /* Remote utility routines for the remote server for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "param.h"
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <sys/wait.h>
  24. #include <sys/ioctl.h>
  25. #include <a.out.h>
  26. #include <sys/file.h>
  27. #include <sgtty.h> 
  28.  
  29. extern int remote_desc; 
  30. extern int remote_debugging; 
  31. extern int kiodebug; 
  32.  
  33. void remote_open(); 
  34. void remote_send(); 
  35. void putpkt(); 
  36. void getpkt(); 
  37.  
  38. void write_ok(); 
  39. void write_enn(); 
  40. void convert_ascii_to_int(); 
  41. void convert_int_to_ascii(); 
  42. void prepare_resume_reply(); 
  43.  
  44. /* Open a connection to a remote debugger.
  45.    NAME is the filename used for communication.  */
  46.  
  47. void
  48. remote_open (name, from_tty)
  49.      char *name;
  50.      int from_tty;
  51. {
  52.   struct sgttyb sg;
  53.  
  54.   remote_debugging = 0;
  55.  
  56.   remote_desc = open (name, O_RDWR);
  57.   if (remote_desc < 0)
  58.     printf("\ncould not open remote device\n"); 
  59.  
  60.   ioctl (remote_desc, TIOCGETP, &sg);
  61.   sg.sg_flags = RAW;
  62.   ioctl (remote_desc, TIOCSETP, &sg);
  63.  
  64.   if (from_tty)
  65.     printf ("Remote debugging using %s\n", name);
  66.   remote_debugging = 1;
  67. }
  68.  
  69. /* Convert hex digit A to a number.  */
  70.  
  71. static int
  72. fromhex (a)
  73.      int a;
  74. {
  75.   if (a >= '0' && a <= '9')
  76.     return a - '0';
  77.   else if (a >= 'a' && a <= 'f')
  78.     return a - 'a' + 10;
  79.   else
  80.     perror ("Reply contains invalid hex digit");
  81. }
  82.  
  83. /* Convert number NIB to a hex digit.  */
  84.  
  85. static int
  86. tohex (nib)
  87.      int nib;
  88. {
  89.   if (nib < 10)
  90.     return '0'+nib;
  91.   else
  92.     return 'a'+nib-10;
  93. }
  94.  
  95. /* Send the command in BUF to the remote machine,
  96.    and read the reply into BUF.
  97.    Report an error if we get an error reply.  */
  98.  
  99. void
  100. remote_send (buf)
  101.      char *buf;
  102. {
  103.   putpkt (buf);
  104.   getpkt (buf);
  105.  
  106.   if (buf[0] == 'E')
  107.     perror ("Remote failure reply: %s", buf);
  108. }
  109.  
  110. /* Send a packet to the remote machine, with error checking.
  111.    The data of the packet is in BUF.  */
  112.  
  113. void
  114. putpkt (buf)
  115.      char *buf;
  116. {
  117.   int i;
  118.   unsigned char csum = 0;
  119.   char buf2[500];
  120.   char buf3[1];
  121.   int cnt = strlen (buf);
  122.   char *p;
  123.  
  124.   if (kiodebug)
  125.     fprintf (stderr, "Sending packet: %s\n", buf);
  126.  
  127.   /* Copy the packet into buffer BUF2, encapsulating it
  128.      and giving it a checksum.  */
  129.  
  130.   p = buf2;
  131.   *p++ = '$';
  132.  
  133.   for (i = 0; i < cnt; i++)
  134.     {
  135.       csum += buf[i];
  136.       *p++ = buf[i];
  137.     }
  138.   *p++ = '#';
  139.   *p++ = tohex ((csum >> 4) & 0xf);
  140.   *p++ = tohex (csum & 0xf);
  141.  
  142.   /* Send it over and over until we get a positive ack.  */
  143.  
  144.   do {
  145.     write (remote_desc, buf2, p - buf2);
  146.     read (remote_desc, buf3, 1);
  147.   } while (buf3[0] != '+');
  148. }
  149.  
  150. static int
  151. readchar ()
  152. {
  153.   char buf[1];
  154.   while (read (remote_desc, buf, 1) != 1) ;
  155.   return buf[0] & 0x7f;
  156. }
  157.  
  158. /* Read a packet from the remote machine, with error checking,
  159.    and store it in BUF.  */
  160.  
  161. void
  162. getpkt (buf)
  163.      char *buf;
  164. {
  165.   char *bp;
  166.   unsigned char csum, c, c1, c2;
  167.   extern kiodebug;
  168.  
  169.   while (1)
  170.     {
  171.       csum = 0; 
  172.       while ((c = readchar()) != '$');
  173.  
  174.       bp = buf;
  175.       while (1)
  176.     {
  177.       c = readchar ();
  178.       if (c == '#')
  179.         break;
  180.       *bp++ = c;
  181.       csum += c;
  182.     }
  183.       *bp = 0;
  184.  
  185.       c1 = fromhex (readchar ());
  186.       c2 = fromhex (readchar ());
  187.       if (csum == (c1 << 4) + c2)
  188.         break;
  189.  
  190.       printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  191.           (c1 << 4) + c2, csum, buf);
  192.       write (remote_desc, "-", 1);
  193.     }
  194.  
  195.   write (remote_desc, "+", 1);
  196.  
  197.   if (kiodebug)
  198.     fprintf (stderr,"Packet received :%s\n", buf);
  199. }
  200.  
  201.  
  202. void 
  203. write_ok(buf)
  204.     char *buf; 
  205. {
  206.     buf[0] = 'O';
  207.     buf[1] = 'k';
  208.     buf[2] = '\0';
  209. }
  210.  
  211. void 
  212. write_enn(buf)
  213.     char *buf; 
  214. {
  215.     buf[0] = 'E';
  216.     buf[1] = 'N';
  217.     buf[2] = 'N';
  218.     buf[3] = '\0';
  219. }
  220.  
  221. void
  222. convert_int_to_ascii(from,to,n)
  223. char *from, *to; int n; 
  224. {
  225.      int nib ; 
  226.     char ch; 
  227.     while( n-- )
  228.     {
  229.         ch = *from++;         
  230.         nib = ((ch & 0xf0) >> 4)& 0x0f; 
  231.         *to++ = tohex(nib); 
  232.         nib = ch & 0x0f; 
  233.         *to++ = tohex(nib); 
  234.     } 
  235.     *to++ = 0; 
  236. }
  237.  
  238.  
  239. void
  240. convert_ascii_to_int(from,to,n)
  241. char *from, *to; int n;  
  242. {
  243.      int nib1,nib2 ; 
  244.     while( n-- )
  245.     {
  246.         nib1 = fromhex(*from++); 
  247.         nib2 = fromhex(*from++); 
  248.         *to++ = (((nib1 & 0x0f)<< 4)& 0xf0) | (nib2 & 0x0f); 
  249.     } 
  250. }
  251.  
  252. void
  253. prepare_resume_reply(buf,status,signal)
  254. char *buf ,status; 
  255. unsigned char signal; 
  256. {
  257.      int nib; 
  258.     char ch; 
  259.  
  260.     *buf++ = 'S';  
  261.     *buf++ = status;  
  262.     nib = ((signal & 0xf0) >> 4) ; 
  263.     *buf++ = tohex(nib); 
  264.     nib = signal & 0x0f; 
  265.     *buf++ = tohex(nib); 
  266.     *buf++ = 0; 
  267. }
  268.  
  269. void 
  270. decode_m_packet(from,mem_addr_ptr,len_ptr)
  271. char *from;
  272. unsigned int *mem_addr_ptr, *len_ptr; 
  273. {
  274.     int i = 0, j = 0 ; 
  275.     char ch; 
  276.     *mem_addr_ptr = *len_ptr = 0; 
  277.     /************debugging begin************/ 
  278.     printf("\nIn decode_m_packet"); 
  279.     /************debugging end************/ 
  280.  
  281.     while((ch = from[i++]) != ',') 
  282.     { 
  283.         *mem_addr_ptr = *mem_addr_ptr << 4; 
  284.         *mem_addr_ptr |= fromhex(ch) & 0x0f; 
  285.     } 
  286.     /************debugging begin************/ 
  287.     printf("\nFinished mem_addr part"); 
  288.     /************debugging end************/ 
  289.  
  290.     for(j=0; j < 4; j++) 
  291.     { 
  292.         if((ch = from[i++]) == 0)  
  293.             break; 
  294.         *len_ptr = *len_ptr << 4; 
  295.         *len_ptr |= fromhex(ch) & 0x0f; 
  296.     } 
  297.     /************debugging begin************/ 
  298.     printf("\nFinished len_ptr part"); 
  299.     /************debugging end************/ 
  300. }
  301.  
  302. void 
  303. decode_M_packet(from,mem_addr_ptr,len_ptr,to)
  304. char *from, *to;
  305. unsigned int *mem_addr_ptr, *len_ptr; 
  306. {
  307.     int i = 0, j = 0 ; 
  308.     char ch; 
  309.     *mem_addr_ptr = *len_ptr = 0; 
  310.     /************debugging begin************/ 
  311.     printf("\nIn decode_M_packet"); 
  312.     /************debugging end************/ 
  313.  
  314.     while((ch = from[i++]) != ',') 
  315.     { 
  316.         *mem_addr_ptr = *mem_addr_ptr << 4; 
  317.         *mem_addr_ptr |= fromhex(ch) & 0x0f; 
  318.     } 
  319.     /************debugging begin************/ 
  320.     printf("\nFinished mem_addr part: memaddr = %x",*mem_addr_ptr); 
  321.     /************debugging end************/ 
  322.  
  323.     while((ch = from[i++]) != ':') 
  324.     { 
  325.         *len_ptr = *len_ptr << 4; 
  326.         *len_ptr |= fromhex(ch) & 0x0f; 
  327.     } 
  328.     /************debugging begin************/ 
  329.     printf("\nFinished len_ptr part: len = %d",*len_ptr); 
  330.     /************debugging end************/ 
  331.  
  332.     convert_ascii_to_int(&from[i++],to,*len_ptr); 
  333.  
  334.     /************debugging begin************/ 
  335.     printf("\nmembuf : %x",*(int *)to); 
  336.     /************debugging end************/ 
  337. }
  338.  
  339.